package com.li.Dao;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.li.hibernate.pojo.ShUser;
import com.li.util.PageHibernate;

public class UserDao extends HibernateDaoSupport {

	public ShUser findByUsername(String username) {
		String hql = "from ShUser where username = ?";
		List<ShUser> list = this.getHibernateTemplate().find(hql, username);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	
	public void save(ShUser user) {
		this.getHibernateTemplate().save(user);
	}

	
	public ShUser findBycode(String id) {
		String hql = "from ShUser where userid = ?";
		List<ShUser> list = this.getHibernateTemplate().find(hql, id);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	
	public void update(ShUser existUser) {
		this.getHibernateTemplate().update(existUser);
	}

	
	public ShUser login(ShUser user) {
		String hql = "from User where username = ? and userpwd = ? ";
		List<ShUser> list = this.getHibernateTemplate().find(hql,
				user.getUsername(), user.getUserpwd() );
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	public int findCount() {
		String hql = "select count(*) from ShUser";
		List<Long> list = this.getHibernateTemplate().find(hql);
		if (list != null && list.size() > 0) {
			return list.get(0).intValue();
		}
		return 0;
	}

	public List<ShUser> findByPage(int begin, int limit) {
		String hql = "from ShUser";
		List<ShUser> list = this.getHibernateTemplate().execute(
				new PageHibernate<ShUser>(hql, null, begin, limit));
		return list;
	}

	public ShUser findByUid(Integer userid) {
		return this.getHibernateTemplate().get(ShUser.class, userid);
	}

	public void delete(ShUser existUser) {
		this.getHibernateTemplate().delete(existUser);
	}
	
}
